#libraries
library(dplyr)
library(readxl)
library(ggplot2)
library(scales) 
library(RColorBrewer)
library(hrbrthemes)
library(patchwork)

#import data
ÖVP <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_time.xlsx")
SPÖ <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_time.xlsx")

ÖVP_CC <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_Quereinsteiger.xlsx")
SPÖ_CC <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_Quereinsteiger.xlsx")

#transform 
ÖVP_CC[1] <- NULL
SPÖ_CC[1] <- NULL

                        ###############
                        ###Newcomers###
                        ###############

#identify newcomers

ÖVP <- ÖVP %>% 
  group_by(LastName, FirstName) %>% 
  mutate(Row = row_number())
SPÖ <- SPÖ %>% 
  group_by(LastName, FirstName) %>% 
  mutate(Row = row_number())

ÖVP$Newcomer <-
  with(ÖVP,ifelse(Row > 1,"incumbent","newcomer"))
SPÖ$Newcomer <-
  with(SPÖ,ifelse(Row > 1,"incumbent","newcomer"))

#manually change in case of legislative period 5

ÖVP[ÖVP$GP_lat=="V",]$Newcomer<-"newcomer"
SPÖ[SPÖ$GP_lat=="V",]$Newcomer<-"newcomer" 

ÖVP[ÖVP$LastName=="Eichinger" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Gierlinger" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Kraus" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Kunschak" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Mayrhofer" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Raab" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Roth" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Seidl" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Steinegger" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"
ÖVP[ÖVP$LastName=="Weidenholzer" & ÖVP$GP_lat=="V",]$Newcomer<-"incumbent"

SPÖ[SPÖ$LastName=="Böhm" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Brachmann" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Flossmann" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Frühwirth" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Jiricek" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Koref" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Lagger" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Proft" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Renner" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Schneeberger" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Seitz" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"
SPÖ[SPÖ$LastName=="Stika" & SPÖ$GP_lat=="V",]$Newcomer<-"incumbent"


                    #########################
                    ###Add Career Changers###
                    #########################

ÖVP <- ÖVP %>%
  select(LastName,FirstName,Wahlpartei,GP_lat,GP_dt,Newcomer)
SPÖ <- SPÖ %>%
  select(LastName,FirstName,Wahlpartei,GP_lat,GP_dt,Newcomer)

ÖVP <- ÖVP %>%
  right_join(ÖVP_CC, by=c("LastName","FirstName"))
SPÖ <- SPÖ %>%
  right_join(SPÖ_CC, by=c("LastName","FirstName"))

merged.data <- rbind(ÖVP, SPÖ)

rm(list=setdiff(ls(), "merged.data"))

                      #########################
                      ###Visualize#############
                      #########################


#prepare for visualization

merged.data$CareerChanger <- as.factor(merged.data$CareerChanger)
merged.data$CareerChanger <- relevel(merged.data$CareerChanger, "pre-parl. pol. career: yes")

merged.data$GP_dt <- as.factor(merged.data$GP_dt)
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="5"] <- "5 (1945-49)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="6"] <- "6 (1949-53)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="7"] <- "7 (1953-56)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="8"] <- "8 (1956-59)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="9"] <- "9 (1959-62)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="10"] <- "10 (1962-66)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="11"] <- "11 (1966-70)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="12"] <- "12 (1970-71)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="13"] <- "13 (1971-75)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="14"] <- "14 (1975-79)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="15"] <- "15 (1979-83)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="16"] <- "16 (1983-86)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="17"] <- "17 (1986-90)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="18"] <- "18 (1990-94)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="19"] <- "19 (1994-96)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="20"] <- "20 (1996-99)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="21"] <- "21 (1999-02)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="22"] <- "22 (2002-06)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="23"] <- "23 (2006-08)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="24"] <- "24 (2008-13)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="25"] <- "25 (2013-17)"
levels(merged.data$GP_dt)[levels(merged.data$GP_dt)=="26"] <- "26 (2017-19)"

#visualize

p1 <- ggplot(merged.data,aes(x=GP_dt,fill=Newcomer)) + geom_bar(position='fill') +
  scale_fill_manual(values=c('palegreen4','palegreen2'))+
  facet_wrap(~Wahlpartei,ncol=2) + 
  scale_y_continuous(labels=percent) + ylab('Percent')+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=15),
        axis.text.y  = element_text(vjust=0.0,size=15),
        strip.text.x = element_text(size = 20,face = "bold"),
        legend.text=element_text(size=17))+
  guides(fill=guide_legend(title=""))+
  labs(y = "", x= "", title="")+
  theme(legend.position="bottom")
plot(p1)

p2 <-  ggplot(merged.data,aes(x=GP_dt,fill=CareerChanger)) + geom_bar(position='fill') +      
  scale_fill_manual(values=c('palegreen4','palegreen2'))+
  facet_wrap(~Wahlpartei,ncol=2) + 
  scale_y_continuous(labels=percent) + ylab('Percent')+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=15),
        axis.text.y  = element_text(vjust=0.0,size=15),
        strip.text.x = element_text(size = 20,face = "bold"),
        legend.text=element_text(size=17))+
  guides(fill=guide_legend(title=""))+
  labs(y = "", x= "", title="")+
  theme(legend.position="bottom")
plot(p2)

#save figure

p1/p2 
ggsave("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure1.png", width=14.0,height=14.0,units="in")